//
//  TomCatView.m
//  UI5_UIControl课后作业
//
//  Created by dllo on 15/11/12.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "TomCatView.h"

@implementation TomCatView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubviews];
    }
    return self;
}

- (void)createSubviews
{
    self.backgroundColor = [UIColor lightGrayColor];
    UIImageView *imageV = [[UIImageView alloc] initWithFrame:self.frame];
    imageV.image = [UIImage imageNamed:@"angry_00.jpg"];
    [self addSubview:imageV];
    [imageV release];
    
    
    
    UIButton *drinkB = [UIButton buttonWithType:UIButtonTypeCustom];
    drinkB.frame = CGRectMake(70, 20, 50, 50);
    [drinkB setImage:[UIImage imageNamed:@"drink.png"] forState:UIControlStateNormal];
    [drinkB addTarget:self action:@selector(drinkBAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:drinkB];
    
    UIButton *eatB = [UIButton buttonWithType:UIButtonTypeCustom];
    eatB.frame = CGRectMake(150, 20, 50, 50);
    [eatB setImage:[UIImage imageNamed:@"eat.png"] forState:UIControlStateNormal];
    [eatB addTarget:self action:@selector(eatBAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:eatB];
    
    UIButton *pieB = [UIButton buttonWithType:UIButtonTypeCustom];
    pieB.frame = CGRectMake(230, 20, 50, 50);
    [pieB setImage:[UIImage imageNamed:@"pie.png"] forState:UIControlStateNormal];
    [pieB addTarget:self action:@selector(pieBAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:pieB];
    
    UIView *FootLeft = [[UIView alloc] initWithFrame:CGRectMake(180, 580, 60, 60)];
    FootLeft.backgroundColor = [UIColor clearColor];
    [self addSubview:FootLeft];
    [FootLeft release];
    
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [FootLeft addGestureRecognizer:tapGR];
    [tapGR release];
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(300, 30, 50, 50);
    [button setTitle:@"返回" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(returnButton:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button];
    
}

- (void)returnButton:(UIButton *)sender
{
    [self removeFromSuperview];
}

- (void)drinkBAction:(UIButton *)sender
{
    
    NSMutableArray *imageArr = [NSMutableArray array];
    for (NSInteger i = 0; i < 81; i++) {
        NSString *fileName = [NSString stringWithFormat:@"drink_%02ld.jpg", i];
        UIImage *image = [UIImage imageNamed:fileName];
        [imageArr addObject:image];
    }
    
    UIImageView *imageV = [[UIImageView alloc] initWithFrame:self.frame];
    
    imageV.animationImages = imageArr;
    imageV.animationRepeatCount = 1;
    [imageV startAnimating];
    [self addSubview:imageV];
    [imageV release];
    
    
}

- (void)eatBAction:(UIButton *)sender
{
    NSMutableArray *imageArr = [NSMutableArray array];
    for (NSInteger i = 0; i < 40; i++) {
        NSString *fileName = [NSString stringWithFormat:@"eat_%02ld.jpg", i];
        UIImage *image = [UIImage imageNamed:fileName];
        [imageArr addObject:image];
    }
   
    UIImageView *imageV = [[UIImageView alloc] initWithFrame:self.frame];
    imageV.animationImages = imageArr;
    imageV.animationRepeatCount = 1;
    [imageV startAnimating];
    [self addSubview:imageV];
    [imageV release];
    
    
}

- (void)pieBAction:(UIButton *)sender
{
    NSMutableArray *imageArr = [NSMutableArray array];
    for (NSInteger i = 0; i < 24; i++) {
        NSString *fileName = [NSString stringWithFormat:@"pie_%02ld.jpg", i];
        UIImage *image = [UIImage imageNamed:fileName];
        [imageArr addObject:image];
        
    }
    
    
    
    
    UIImageView *imageV = [[UIImageView alloc] initWithFrame:self.frame];
    imageV.animationImages = imageArr;
    imageV.animationRepeatCount = 1;
    imageV.animationDuration = 1;
    [imageV startAnimating];
    [self addSubview:imageV];
    [imageV release];
    
    
    
    
}

- (void)tapAction:(UITapGestureRecognizer *)sender
{
    NSMutableArray *imageArr = [NSMutableArray array];
    for (NSInteger i = 0; i < 30; i++) {
        NSString *fileName = [NSString stringWithFormat:@"footLeft_%02ld.jpg", i];
        UIImage *image = [UIImage imageNamed:fileName];
        [imageArr addObject:image];
    }
    
    UIImageView *imageV = [[UIImageView alloc] initWithFrame:self.frame];
    imageV.animationImages = imageArr;
    imageV.animationRepeatCount = 1;
    [imageV startAnimating];
    [self addSubview:imageV];
    [imageV release];
    
    
}



@end
